import random
import string
import time
from collections import deque

from loop import coroutine, YieldLoop


@coroutine
def producer(q):
    while True:
        good = "".join(random.sample(string.ascii_letters + string.digits, 8))
        q.append(good)
        cnt = len(q)
        print("producer produce good, cnt = {}".format(cnt))
        if cnt > 0:
            yield


@coroutine
def consumer(q):
    while True:
        while len(q) <= 0:
            yield
        good = q.popleft()
        print("consumer consume good:{}, cnt={}".format(good, len(q)))
        time.sleep(1)


if __name__ == '__main__':
    q1 = deque()
    YieldLoop.instance().add_coroutine(producer(q1))
    YieldLoop.instance().add_coroutine(producer(q1))
    YieldLoop.instance().add_coroutine(producer(q1))
    YieldLoop.instance().add_coroutine(consumer(q1))
    YieldLoop.instance().run_until_complete()